<?php

/*
 * Camelot PHP Tools
 *
 * @version 2.0
 * @author Bendsoft
 * @package PHP Tools
 * @subpackage Camelot
 * @license FreeBSD License (www.bendsoft.com/licensing/)
 *
 * Note! 
 * Set up settings.php before using this file
 */

header('Content-Type: text/html; charset=utf-8');
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Load all required files for Camelot PHP Tools
require_once '../loader.php';
require_once '../twig/lib/Twig/Autoloader.php';
?>

<?php include 'base/head.php'; ?>

<p>Enter SQL command, in example SHOW TABLES</p>
<form method="post">
    <input type="text" name="command" value="<?php echo $_POST['command']; ?>" style="width: 500px;" /> 
    <input type="submit">
</form>

<?php

if ($_POST) {
    
    // Stopwatch
    $time_pre = microtime(true);
    
    try {
        // Build the command object
        $command = new SharePointSqlCommand();
        $command->connection_name = "SharePointConnection1";
        $command->sql = $_POST['command'];

        // Execute the command
        $SharePointQuery = new SharePointQuery($command);

        // Only render the template if commandStatus is true
        if ($SharePointQuery->commandStatus) {

            // Render the result using Twig        
            Twig_Autoloader::register();
            $loader = new Twig_Loader_Filesystem('../templates');
            $twig = new Twig_Environment($loader);
            $template = $twig->loadTemplate('sp2007.html');
            echo $template->render(array(
                'list_header' => $SharePointQuery->CamelotSoap->_sorted->_schema->display_name,
                'list_content' => $SharePointQuery->CamelotSoap->_sorted->_content
            ));
        } else {
            // Some form of exception occured. 
            // We will only reach this if SILENT_EXCEPTIONS is set to TRUE
            echo "<p>Silent exceptions is ON, this error message is forced to appear in this example</p>";
            $message = $SharePointQuery->exception->message;
            echo "<h2>Exception: $message</h2>";
            pr($SharePointQuery);
        }
    } catch (CamelotException $exc) {
        echo "<h2>Exception: $exc->message</h2>";
        pr($exc);
    }
    
    // ...end stopwatch
    $time_post = microtime(true);
    $exec_time = $time_post - $time_pre;
    echo "<p class=\"comment\">Query executed and data transfered in $exec_time seconds</p>";
}
?>

<?php include 'base/foot.html'; ?>
